package com.googlecode.jsimplemarket.service;

import com.googlecode.jsimplemarket.repository.PlataRepository; 
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service(value = "plataService")
public class PlataService {
	@Autowired
	private PlataRepository plataRepository;
	
	
    private Date getStartDate(Date date) {
        GregorianCalendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.set(GregorianCalendar.HOUR_OF_DAY, 0);
        calendar.set(GregorianCalendar.MINUTE, 0);
        calendar.set(GregorianCalendar.SECOND, 0);
        calendar.set(GregorianCalendar.MILLISECOND, 0);
        
        return calendar.getTime();
    }
    
    private Date getEndDate(Date date) {
        GregorianCalendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.set(GregorianCalendar.HOUR_OF_DAY, 23);
        calendar.set(GregorianCalendar.MINUTE, 59);
        calendar.set(GregorianCalendar.SECOND, 59);
        calendar.set(GregorianCalendar.MILLISECOND, 999);
        
        return calendar.getTime();
    }
    
    private GregorianCalendar getTodayCalendar() {
        GregorianCalendar calendar = new GregorianCalendar();
        calendar.set(GregorianCalendar.HOUR_OF_DAY, 0);
        calendar.set(GregorianCalendar.MINUTE, 0);
        calendar.set(GregorianCalendar.SECOND, 0);
        //calendar.set(GregorianCalendar.AM_PM, GregorianCalendar.AM);
        calendar.set(GregorianCalendar.MILLISECOND, 0);
        return calendar;
    }
    
    private Date getStartCalendar() {
        GregorianCalendar calendar = new GregorianCalendar();
        calendar.set(2014, 10, 01);
        calendar.set(GregorianCalendar.MILLISECOND, 0);
        return calendar.getTime();
    }
    
    
    private Date getTodayDate() {
        return getTodayCalendar().getTime();
    }
    
    private Date addDays(Date date, int days) {
        GregorianCalendar cal = new GregorianCalendar();
        cal.setTime(date);
        cal.add(Calendar.DATE, days);
                 
        return cal.getTime();
    }
    
    private Date get7DayDate() {
        GregorianCalendar calendar = getTodayCalendar();
        calendar.add(GregorianCalendar.DATE, -30);
        
        return calendar.getTime();
    }
    
    private Date getLastMonthDate() {
        GregorianCalendar calendar = getTodayCalendar();
        calendar.set(GregorianCalendar.DATE, 1);
        
        return calendar.getTime();
    }
	
	
    public List<Object[]> querySumIncasariByDay() {
        //Date dataInceput = getDataInceputZi(data);
        //Date dataSfarsit = getDataSfarsitZi(data);
    	
    	Date sevenDay = get7DayDate();
        return plataRepository.querySumIncasariByDay(sevenDay);
    }
    
    public List<Object[]> querySumIncasariByMonth() {
    	Date dayStart = getStartCalendar();
        return plataRepository.querySumIncasariByMonth(dayStart);
    }

}
